package String;

public class ReverseStr541 {
    public String reverseStr(String s, int k) {
        if (s == null) {
            return s;
        }
        if (s.length() < k) {
            String s1 = new StringBuilder(s).reverse().toString();
            return s1;
        }
        int i = 0;
        while (i < s.length()) {
            if (i + k <= s.length()) {
                s = reverse(s, i, i + k - 1);
            }else{
                s = reverse(s,i,s.length()-1);
            }
            i = i + 2 * k;
        }
        return s;
    }

    public String reverse(String s, int i, int j) {
        char[] ch = s.toCharArray();
        for (; i < j; i++, j--) {
            char temp = ch[i];
            ch[i] = ch[j];
            ch[j] = temp;
        }
        return new String(ch);
    }


}


